﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Network;
using DcsGauges.Shared.Packets;
using DcsGauges.Shared.DataStructures;
using DcsGauges.Shared.Network;

namespace DcsGauges.Data
{
    class ServerDataModel : IDcsA10DataModel
    {
        #region Fields / Properties
        
        private IServerChannel                              dataChannel                 = null;
        private Dictionary<DcsProperty, float>              data                        = new Dictionary<DcsProperty,float>();

        #endregion


        #region Constructors

        public ServerDataModel(IServerChannel dataChannel)
        {
            this.dataChannel                                = dataChannel;
            this.dataChannel.DataPacketReceivedEvent        += OnDataChannelDataPacketReceivedEvent;
        }

        #endregion


        #region Interface Implementation

        public float GetProperty(DcsProperty prop)
        {
            float foundValue           = 0.0f;
            this.data.TryGetValue(prop, out foundValue);
            return foundValue;
        }

        public void Update()
        {
            // server data model has nothing to do here, because server pushes data into this model
        }

        public void IssueCommand(DcsProperty prop, float argValue)
        {
            CommandsPacket packet               = new CommandsPacket(prop, argValue);
            this.dataChannel.Send(packet);
        }

        #endregion


        #region Event Handlers

        void OnDataChannelDataPacketReceivedEvent(object sender, PacketReceivedEventArgs<Shared.Packets.DataPacket> e)
        {
            foreach (var v in e.Packet.Values)
            { 
                this.data[v.Item1]      = v.Item2;
            }
        }

        #endregion
    }
}
